package domain.message ;

import java.util.Calendar ;
import java.util.Date ;

import org.compass.annotations.Searchable ;
import org.compass.annotations.SearchableProperty ;

import persistence.Persistent ;
import presentation.UIPost ;
import util.DateTools ;

/**
 * class post. represent a message inside a thread.
 */
@Searchable
public class Post extends Persistent implements UIPost
{
	private static final long serialVersionUID = -5450696763595007006L ;

	@SearchableProperty (name = "writerId")
	/**id of user who wrote the message**/
	private long writerId;

	@SearchableProperty (name = "message")
	/**content of message**/
	private String message;

	@SearchableProperty (name = "date", format = DateTools.GLOBAL_DATE_FORMAT)
	/** date of post creation **/
	private Date date;

	@SearchableProperty (name = "threadParentId")
	/** The thread holding the post **/
	private long threadParentId;

	/**
	 * constructor.
	 * @param id id of this post.
	 * @param writerId - id of writer that wrote the message
	 * @param parentId - the id of the parent thread
	 * @param msg - the message
	 */
	public Post(final long id, final String msg, final long writerId, final long parentId)
	{
		super(id) ;
		this.message = msg ;
		this.writerId = writerId ;
		this.threadParentId = parentId;
		this.date = Calendar.getInstance().getTime() ;
	}

	// for serialization
	@SuppressWarnings("unused")
	private Post()
	{
		super(Persistent.INVALID_ID) ;
		this.date = null ;
		this.message = null ;
		this.writerId = Persistent.INVALID_ID ;
	}

	/**
	 * @param text the new message after editing
	 */
	public void edit(final String text)
	{
		this.setMessage(text) ;
	}

	/**
	 * @return the date when this post was created
	 */
	public Date getDate()
	{
		this.lockRead() ;
		try
		{
			return this.date ;
		}
		finally
		{
			this.unlockRead() ;
		}
	}

	/**
	 * @return the content of the post
	 */
	public String getMessage()
	{
		this.lockRead() ;
		try
		{
			return this.message ;
		}
		finally
		{
			this.unlockRead() ;
		}
	}

	/**
	 * @return id of parent thread.
	 */
	public long getThreadParentId()
	{
		this.lockRead() ;
		try
		{
			return this.threadParentId ;
		}
		finally
		{
			this.unlockRead() ;
		}
	}

	/**
	 * @return id of the user who wrote this post.
	 */
	public long getWriterId()
	{
		this.lockRead() ;
		try
		{
			return this.writerId ;
		}
		finally
		{
			this.unlockRead() ;
		}
	}

	/**
	 * @param date date in which this post was last modified.
	 * for statistics: // TODO make protected.
	 */
	public void setDate(final Date date)
	{
		this.lockWrite() ;
		try
		{
			this.date = date ;
		}
		finally
		{
			this.unlockWrite() ;
		}
	}

	/**
	 * function for debugging-return string with posts info
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString()
	{
		return super.toString() + " " + this.getWriterId() + ": " + this.getMessage() ;
	}

	/**
	 * @param message contents of this post.
	 */
	protected void setMessage(final String message)
	{
		this.lockWrite() ;
		try
		{
			this.message = message ;
		}
		finally
		{
			this.unlockWrite() ;
		}
	}

	/**
	 * @param writerId is of the authod of this post.
	 */
	protected void setWriterId(final long writerId)
	{
		this.lockWrite() ;
		try
		{
			this.writerId = writerId ;
		}
		finally
		{
			this.unlockWrite() ;
		}
	}

	@SuppressWarnings("unused")
	private void setThreadParentId(final long threadParentId)
	{
		this.threadParentId = threadParentId ;
	}
}
